<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="MySQL支持的函数:   操作字符串的函数 操作数值的函数 操作日期的函数 获取系统信息的函数">
<meta name="keywords" content="Android、iOS、Web、React Native">
<meta property="og:type" content="article">
<meta property="og:title" content="MySQL-基础---常用函数">
<meta property="og:url" content="http://yoursite.com/blog/2018/08/06/MySQL/MySQL-基础---常用函数/index.html">
<meta property="og:site_name" content="凌浩雨_博客">
<meta property="og:description" content="MySQL支持的函数:   操作字符串的函数 操作数值的函数 操作日期的函数 获取系统信息的函数">
<meta property="og:locale" content="default">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/3110861-6bb422b240cbc78c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/3110861-33a6fc1b2ad3ea2d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/3110861-598a974aaa994b95.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/3110861-a5e0ccfeb77bbfc3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/3110861-f5e196db86f3d3c2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/3110861-44bf8cca6fd75c98.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/3110861-0b6dddef11641080.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2018-08-06T10:35:42.248Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="MySQL-基础---常用函数">
<meta name="twitter:description" content="MySQL支持的函数:   操作字符串的函数 操作数值的函数 操作日期的函数 获取系统信息的函数">
<meta name="twitter:image" content="http://upload-images.jianshu.io/upload_images/3110861-6bb422b240cbc78c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/blog/2018/08/06/MySQL/MySQL-基础---常用函数/"/>





  <title>MySQL-基础---常用函数 | 凌浩雨_博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">凌浩雨_博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/blog/blog/2018/08/06/MySQL/MySQL-基础---常用函数/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="凌浩雨">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="凌浩雨_博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">MySQL-基础---常用函数</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-08-06T18:01:39+08:00">
                2018-08-06
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>MySQL支持的函数: </p>
<ul>
<li>操作字符串的函数</li>
<li>操作数值的函数</li>
<li>操作日期的函数</li>
<li>获取系统信息的函数<a id="more"></a>
</li>
</ul>
<ol>
<li>字符串函数</li>
</ol>
<p><img src="http://upload-images.jianshu.io/upload_images/3110861-6bb422b240cbc78c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="字符串函数.png"></p>
<p>(1) 合并字符串函数CONCAT()和CONCAT_WS()<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">select concat(&apos;my&apos;,&apos;s&apos;,&apos;ql&apos;) &apos;合并后字符串&apos;;</span><br><span class="line">select concat_ws(&apos;-&apos;,&apos;my&apos;,&apos;sql&apos;) &apos;合并后字符串&apos;;</span><br></pre></td></tr></table></figure></p>
<p>(2) 比较字符串大小函数STRCMP()<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">select strcmp(&apos;abc&apos;, &apos;abd&apos;),</span><br><span class="line">         strcmp(&apos;abc&apos;,&apos;abc&apos;),</span><br><span class="line">         strcmp(&apos;abc&apos;,&apos;abb&apos;);</span><br></pre></td></tr></table></figure></p>
<p>(3) 获取字符串长度函数LENGTH()和字符数函数CHAR_LENGTH()<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">select &apos;MySQL&apos; &apos;英文字符串&apos;,</span><br><span class="line">         length(&apos;MySQL&apos;) &apos;字符串字节长度&apos;,</span><br><span class="line">         &apos;麻再挺&apos; &apos;中文字符串&apos;,</span><br><span class="line">         length(&apos;麻再挺&apos;) &apos;字符串字节长度&apos;;</span><br></pre></td></tr></table></figure></p>
<p>(4) 实现大小写转换函数UPPER()/UCASE()和LOWER()<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">select UPPER(&apos;mysql&apos;) &apos;转换后字符串&apos;,</span><br><span class="line">        UCASE(&apos;mysql&apos;) &apos;转换后字符串&apos;,</span><br><span class="line">        LOWER(&apos;MYSQL&apos;) &apos;转换后字符串&apos;;</span><br></pre></td></tr></table></figure></p>
<p>(5) 查找字符串</p>
<ul>
<li><p>返回字符串位置的FIND_IN_SET()函数</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">select find_in_set(&apos;mysql&apos;,&apos;oracle,sql server,mysql&apos;) &apos;位置&apos;;</span><br></pre></td></tr></table></figure>
</li>
<li><p>返回指定字符串位置的FIELD()函数</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">select field(&apos;mysql&apos;,&apos;oracle&apos;,&apos;sql server&apos;,&apos;mysql&apos;) &apos;位置&apos;;</span><br></pre></td></tr></table></figure>
</li>
<li><p>返回子字符串相匹配的开始位置<br>函数: LOCATE()、POSITION()、INSTR()</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">select locate(&apos;sql&apos;,&apos;mysql&apos;) &apos;位置&apos;,</span><br><span class="line">         position(&apos;sql&apos; in &apos;mysql&apos;) &apos;位置&apos;,</span><br><span class="line">         instr(&apos;mysql&apos;, &apos;sql&apos;) &apos;位置&apos;;</span><br></pre></td></tr></table></figure>
</li>
<li><p>返回指定位置的字符串的ELT()函数</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">select elt(1,&apos;mysql&apos;,&apos;oracle&apos;,&apos;sql server&apos;) &apos;第一个位置的字符串&apos;;</span><br></pre></td></tr></table></figure>
</li>
<li><p>选择字符串的MAKE_SET()函数</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">select bin(7) &apos;二进制数&apos;,</span><br><span class="line">    make_set(7,&apos;mysql&apos;,&apos;oracle&apos;,&apos;sql server&apos;, &apos;postgresql&apos;) &apos;选取后的字符串&apos;;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>(6) 从现有字符串中截取子字符串</p>
<ul>
<li><p>从左边或右边截取子字符串<br>LEFT(str,num)/RIGHT(str,num)</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">select left(&apos;mysql&apos;,2) &apos;前两个字符串&apos;, right(&apos;mysql&apos;,3) &apos;后三个字符串&apos;;</span><br></pre></td></tr></table></figure>
</li>
<li><p>截取指定位置和长度子字符串<br>SUBSTRING(str,num,len)/MID(str,num,len)</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">select substring(&apos;oraclemysql&apos;,7,5) &apos;截取子字符串&apos;,</span><br><span class="line">         mid(&apos;oraclemysql&apos;,7,5) &apos;截取子字符串&apos;;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>(7) 去除字符串的首尾空格</p>
<ul>
<li><p>去除字符串开始处空格<br>LTRIM(str)</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">select concat(&apos;-&apos;,&apos; mysql &apos;,&apos;-&apos;) &apos;原来的字符串&apos;,</span><br><span class="line">       concat(&apos;-&apos;,ltrim(&apos; mysql &apos;),&apos;-&apos;) &apos;处理后字符串&apos;;</span><br></pre></td></tr></table></figure>
</li>
<li><p>去除字符串结束时空格</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">select concat(&apos;-&apos;,&apos; mysql &apos;,&apos;-&apos;) &apos;原来的字符串&apos;,</span><br><span class="line">       concat(&apos;-&apos;,rtrim(&apos; mysql &apos;),&apos;-&apos;) &apos;处理后字符串&apos;;</span><br></pre></td></tr></table></figure>
</li>
<li><p>去除字符串首尾空格</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">select concat(&apos;-&apos;,&apos; mysql &apos;,&apos;-&apos;) &apos;原来的字符串&apos;,</span><br><span class="line">       concat(&apos;-&apos;,trim(&apos; mysql &apos;),&apos;-&apos;) &apos;处理后字符串&apos;;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>(8) 替换字符串</p>
<ul>
<li><p>使用INSERT()函数</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">select insert(&apos;这是MySQL数据库管理系统&apos;,3,5,&apos;Oracle&apos;) &apos;转换后字符串&apos;;</span><br></pre></td></tr></table></figure>
</li>
<li><p>使用REPLACE()函数</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">select replace(&apos;这是MySQL数据库管理系统&apos;,&apos;MySQL&apos;,&apos;Oracle&apos;) &apos;转换后字符串&apos;;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<ol start="2">
<li>数值函数</li>
</ol>
<p><img src="http://upload-images.jianshu.io/upload_images/3110861-33a6fc1b2ad3ea2d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="常用数值函数.png"></p>
<p>(1) 获取随机数<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">select rand(),rand(3);</span><br></pre></td></tr></table></figure></p>
<p>(2) 获取整数<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">select ceil(4.3), ceiling(-2.5);</span><br><span class="line">select floor(4.3), floor(-2.5);</span><br></pre></td></tr></table></figure></p>
<p>(3) 截取数值函数<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">select truncate(903.53567,2), truncate(903.53567,-1);</span><br></pre></td></tr></table></figure></p>
<p>(4) 四舍五入函数<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">select round(903.53567), round(-903.53567);</span><br></pre></td></tr></table></figure></p>
<ol start="3">
<li>日期和时间函数</li>
</ol>
<p><img src="http://upload-images.jianshu.io/upload_images/3110861-598a974aaa994b95.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="常用日期和时间函数.png"></p>
<p>(1) 获取当前日期和时间</p>
<ul>
<li><p>获取当前日期和时间</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">select now() &apos;now方式&apos;, current_timestamp() &apos;timestamp方式&apos;,</span><br><span class="line">       localtime() &apos;localtime方式&apos;, sysdate() &apos;systemdate当时&apos;;</span><br></pre></td></tr></table></figure>
</li>
<li><p>获取当前日期</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">select curdate() &apos;curdate方式&apos;, current_date() &apos;current_date方式&apos;;</span><br></pre></td></tr></table></figure>
</li>
<li><p>获取当前时间</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">select curtime() &apos;curtime方式&apos;, current_time() &apos;current_time方式&apos;;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>(2) 通过各种方式显示日期和时间</p>
<ul>
<li><p>通过UNIX显示日期和时间</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">select now() &apos;当前时间&apos;,</span><br><span class="line">         unix_timestamp(now()) &apos;unix格式&apos;,</span><br><span class="line">         from_unixtime(unix_timestamp(now())) &apos;普通格式&apos;;</span><br></pre></td></tr></table></figure>
</li>
<li><p>通过UTC方式显示日期和时间</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">select now() &apos;当前日期和时间&apos;,</span><br><span class="line">         utc_date() &apos;utc日期&apos;,</span><br><span class="line">         utc_time() &apos;utc时间&apos;;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>(3)  获取日期和时间各部分值<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">select now() &apos;当前日期和时间&apos;,</span><br><span class="line">         year(now()) &apos;年&apos;,</span><br><span class="line">         quarter(now()) &apos;季度&apos;,</span><br><span class="line">         month(now()) &apos;月&apos;,</span><br><span class="line">         week(now()) &apos;星期&apos;,</span><br><span class="line">         dayofmonth(now()) &apos;天&apos;,</span><br><span class="line">         hour(now()) &apos;小时&apos;,</span><br><span class="line">         minute(now()) &apos;分&apos;,</span><br><span class="line">         second(now()) &apos;秒&apos;;</span><br></pre></td></tr></table></figure></p>
<ul>
<li><p>关于月的函数</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">select now() &apos;当前日期和时间&apos;,</span><br><span class="line">         month(now()) &apos;月&apos;,</span><br><span class="line">         monthname(now()) &apos;月&apos;;</span><br></pre></td></tr></table></figure>
</li>
<li><p>关于星期的函数</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">select now() &apos;当前日期和时间&apos;,</span><br><span class="line">         week(now()) &apos;年中第几个星期&apos;,</span><br><span class="line">         weekofyear(now()) &apos;年中第几个星期&apos;,</span><br><span class="line">         dayname(now()) &apos;星期&apos;,</span><br><span class="line">         dayofweek(now()) &apos;星期&apos;,</span><br><span class="line">         weekday(now()) &apos;星期&apos;;</span><br></pre></td></tr></table></figure>
</li>
<li><p>关于天的函数</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">select now() &apos;当前日期和时间&apos;,</span><br><span class="line">         dayofyear(now()) &apos;年中第几天&apos;,</span><br><span class="line">         dayofmonth(now()) &apos;月中第几天&apos;;</span><br></pre></td></tr></table></figure>
</li>
<li><p>获取指定值的EXTRACT()函数</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">select now() &apos;当前日期和时间&apos;,</span><br><span class="line">         extract(year from now()) &apos;年&apos;,</span><br><span class="line">         extract(month from now()) &apos;月&apos;,</span><br><span class="line">         extract(day from now()) &apos;天&apos;,</span><br><span class="line">         extract(hour from now()) &apos;小时&apos;,</span><br><span class="line">         extract(minute from now()) &apos;分&apos;,</span><br><span class="line">         extract(second from now()) &apos;秒&apos;;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>(4)  计算日期和时间</p>
<ul>
<li><p>与默认日期和时间操作</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"> select now() &apos;当前日期和时间&apos;,</span><br><span class="line">          to_days(now()) &apos;相隔天数&apos;,</span><br><span class="line">          from_days(to_days(now())) &apos;一段时间后日期和时间&apos;;</span><br><span class="line"></span><br><span class="line"># 计算两个日期之间相隔的天数</span><br><span class="line">  select now() &apos;当前日期和时间&apos;,</span><br><span class="line">           datediff(now(), &apos;2016-12-01&apos;) &apos;相隔天数&apos;;</span><br></pre></td></tr></table></figure>
</li>
<li><p>与指定日期和时间操作</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">select curdate() &apos;当前日期&apos;,</span><br><span class="line">         adddate(curdate(), 5) &apos;5天后日期&apos;,</span><br><span class="line">         subdate(curdate(), 5) &apos;5天前日期&apos;;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>ADDDATE(d, INTERVAL expr type)<br>SUBDATE(d, INTERVAL expr type)</p>
<p><img src="http://upload-images.jianshu.io/upload_images/3110861-a5e0ccfeb77bbfc3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="type可选值.png"><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"># 时间操作</span><br><span class="line">select curtime() &apos;当前时间&apos;,</span><br><span class="line">         addtime(curtime(), 5) &apos;5秒后时间&apos;,</span><br><span class="line">         subtime(curtime(), 5) &apos;5秒前时间&apos;;</span><br></pre></td></tr></table></figure></p>
<ol start="4">
<li>系统信息函数</li>
</ol>
<p><img src="http://upload-images.jianshu.io/upload_images/3110861-f5e196db86f3d3c2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="常用 系统信息函数.png"><br>(1) 获取MySQL系统信息<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">select version() &apos;版本号&apos;,</span><br><span class="line">         database() &apos;数据库名&apos;,</span><br><span class="line">         user() &apos;用户名&apos;;</span><br></pre></td></tr></table></figure></p>
<p>(2) 获取AUTO_INCREMENT约束的最后ID值<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">select last_insert_id();</span><br></pre></td></tr></table></figure></p>
<p>(3) 其他函数</p>
<p><img src="http://upload-images.jianshu.io/upload_images/3110861-44bf8cca6fd75c98.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="流程函数.png"></p>
<p><img src="http://upload-images.jianshu.io/upload_images/3110861-0b6dddef11641080.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="特殊功能函数.png"></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/08/06/MySQL/MySQL-基础---存储过程和函数操作/" rel="next" title="MySQL-基础---存储过程和函数操作">
                <i class="fa fa-chevron-left"></i> MySQL-基础---存储过程和函数操作
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/08/06/MySQL/MySQL-基础---数据库/" rel="prev" title="MySQL-基础---数据库">
                MySQL-基础---数据库 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpeg"
                alt="凌浩雨" />
            
              <p class="site-author-name" itemprop="name">凌浩雨</p>
              <p class="site-description motion-element" itemprop="description">专业从事于移动端开发，Android与iOS均有涉及，主做Android开发与React Native开发</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">393</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">凌浩雨</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
